{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fafa596",
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "from IPython.display import HTML\n",
    "\n",
    "additional_js_code=\"\"\"\n",
    "    let json = K3DInstance.getWorld().ObjectsListJson;    \n",
    "    let active = Object.keys(json)[0];\n",
    "    \n",
    "    setInterval(() => {\n",
    "        let newActive;\n",
    "        \n",
    "        Object.keys(json).forEach(function (id) {\n",
    "          if (active === id) {\n",
    "            json[id].opacity = 0;\n",
    "            K3DInstance.reload(json[id], {opacity: 0});            \n",
    "          } else {\n",
    "            json[id].opacity = 1;\n",
    "            K3DInstance.reload(json[id], {opacity: 1});\n",
    "            newActive = id;\n",
    "          }                    \n",
    "        });\n",
    "        \n",
    "        active = newActive;\n",
    "      \n",
    "    }, 2000);\n",
    "    \n",
    "    \"\"\"\n",
    "\n",
    "points_number = 500\n",
    "positions = 50 * np.random.random_sample((points_number,3)) - 25\n",
    "colors = np.random.randint(0, 0xFFFFFF, points_number)\n",
    "\n",
    "plot = k3d.plot(additional_js_code = additional_js_code)\n",
    "points = k3d.points(positions.astype(np.float32), colors.astype(np.uint32), point_size=3.0, shader='flat')\n",
    "plot += points\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c3823a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dynamic code change will not cancel consequences of original one\n",
    "# To cancel that you need to display a plot again.\n",
    "plot.additional_js_code = 'alert(\"A new js script is here!\")' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "966291e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.snapshot_type = 'inline'\n",
    "HTML(plot.get_snapshot(additional_js_code='alert(\"collision?\")'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4da612e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# No! Both codes are concatenated"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
